package com.utility.project.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utility.project.dto.YsExamAnswerDTO;
import com.utility.project.entity.YsExamAnswer;
import com.utility.project.mapper.YsExamAnswerMapper;
import com.utility.project.param.ExamAnswerParam;
import com.utility.project.param.QueryAnswerParam;
import com.utility.project.service.IYsExamAnswerService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yangzhiguo
 * @date 2024-07-29
 * @description 试题答案
 */
@Service
public class YsExamAnswerServiceImpl extends ServiceImpl<YsExamAnswerMapper, YsExamAnswer> implements IYsExamAnswerService {


    @Override
    public List<YsExamAnswerDTO> queryExamAnswerList(QueryAnswerParam queryAnswerParam) {
        List<YsExamAnswerDTO> ysExamAnswerList = new ArrayList<>();
        List<String> paperIds = queryAnswerParam.getPaperIds();
        String username = queryAnswerParam.getUsername();
        if(CollUtil.isNotEmpty(paperIds)){
            paperIds.forEach(paperId->{
                YsExamAnswerDTO answerDTO = new YsExamAnswerDTO();
                Integer paperNum = queryMaxPaperNum(paperId, username);
                List<YsExamAnswer> ysExamAnswers = queryAnswerList(username, paperId, paperNum);
                answerDTO.setPaperId(paperId);
                answerDTO.setAnswerList(ysExamAnswers);
                ysExamAnswerList.add(answerDTO);
            });
        }

        return ysExamAnswerList;
    }

    @Override
    public List<YsExamAnswer> queryAnswerList(String username, String paperId, Integer paperNum){
        return baseMapper.queryAnswerList(username, paperId, paperNum);
    }

    @Override
    public Boolean saveExamAnswer(List<ExamAnswerParam> paramList) {
        List<YsExamAnswer> answerList = new ArrayList<>();
        Assert.notEmpty(paramList, "用户答题记录为空");
        String username = paramList.get(0).getUsername();
        String paperId = paramList.get(0).getPaperId();
        Integer paperNum = queryMaxPaperNum(paperId, username);
        Integer finalPaperNum = (paperNum == null ? 1 : paperNum + 1);
        Date date = new Date();
        paramList.forEach(param ->{
            YsExamAnswer answer = new YsExamAnswer();
            answer.setPaperNums(finalPaperNum);
            answer.setAnswer(param.getAnswer());
            answer.setUsername(param.getUsername());
            answer.setPaperId(param.getPaperId());
            answer.setExamId(param.getExamId());
            answer.setAnswerTime(date);
            answerList.add(answer);
        });
        return saveBatch(answerList);
    }

    @Override
    public Integer queryMaxPaperNum(String paperId, String username) {
        QueryWrapper<YsExamAnswer> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct paper_nums");
        queryWrapper.eq("username", username);
        queryWrapper.eq("paper_id", paperId);
        queryWrapper.orderByDesc("paper_nums");
        List<YsExamAnswer> ysExamAnswers = baseMapper.selectList(queryWrapper);
        if(CollUtil.isEmpty(ysExamAnswers)){
            return null;
        }else{
            return ysExamAnswers.get(0).getPaperNums();
        }
    }


}
